﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataFrameworkAccess;
using Xinkui.Entity.User;
using Xinkui.Entity.AutoGenerator;
using Xinkui.Common;

namespace Xinkui.DataAccess.User {
    public class UserDataAccess {
        public static UnloginUserInfo ViewUnionUser(int userId) {

            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("Query.UserAll.View");
                cmd.SetParameterValue("@UserId", userId);

                return cmd.ExecuteEntity<UnloginUserInfo>();

            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return null;

        }

        public static UnloginUserInfo UnloginUserInfoAdd(UnloginUserInfo entity) {

            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("UnloginUserInfo.Operation.Add");
                cmd.SetParameterValue("@TempUserId", entity.TempUserId);
                cmd.SetParameterValue("@Source", entity.Source);
                cmd.SetParameterValue("@CompanyName", entity.CompanyName);
                cmd.SetParameterValue("@FirstName", entity.FirstName);
                cmd.SetParameterValue("@LastName", entity.LastName);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@CountryID", entity.CountryID);
                cmd.SetParameterValue("@PhoneNum", entity.PhoneNum);
                cmd.SetParameterValue("@FaxNum", entity.FaxNum);
                cmd.SetParameterValue("@CreateDate", entity.CreateDate);
                cmd.ExecuteNonQuery();
                entity.TempUserId = (int)cmd.GetParameterValue("@TempUserId");
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return entity;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>大于0为用户ID，否则登录失败</returns>
        public static LoginUserEntity UserLogin(string userName, string password) {
            DataCommand command = DataCommandManager.GetDataCommand("Query.User.Login");
            command.SetParameterValue("@UserName", userName);
            command.SetParameterValue("@Password", password);
            return command.ExecuteEntity<LoginUserEntity>();
        }


        public static LoginUserEntity UserLogin(int userId) {
            DataCommand command = DataCommandManager.GetDataCommand("Query.User.ViewById");
            command.SetParameterValue("@UserId", userId);
            return command.ExecuteEntity<LoginUserEntity>();
        }


        public static bool UserNameExists(string userName) {
            DataCommand command = DataCommandManager.GetDataCommand("Query.User.UserExists");
            command.SetParameterValue("@Username", userName);
            object o = command.ExecuteScalar();
            if (o != null) {
                return ((int)o) > 0;
            }
            else { return false; }

        }

        public static UserInfo_Entity UserInfo_ViewByCompany(System.Int32 UserID) {
            UserInfo_Entity c = default(UserInfo_Entity);
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("UserInfo_ViewByCompany");
                cmd.SetParameterValue("@CompanyId", UserID);
                c = cmd.ExecuteEntity<UserInfo_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return c;
        }


        public static bool Register(UserInfoMaintain_Entity user, CompanyInfoMaintain_Entity company) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("Operator.User.Register");
                cmd.SetParameterValue("@UserID", user.UserID);
                cmd.SetParameterValue("@CompanyID", user.CompanyID);
                cmd.SetParameterValue("@UserName", user.UserName);
                cmd.SetParameterValue("@Password", user.Password);
                cmd.SetParameterValue("@FirstName", user.FirstName);
                cmd.SetParameterValue("@LastName", user.LastName);
                cmd.SetParameterValue("@Email", user.Email);
                cmd.SetParameterValue("@Gender", user.Gender);
                cmd.SetParameterValue("@Department", user.Department);
                cmd.SetParameterValue("@JobTitle", user.JobTitle);
                cmd.SetParameterValue("@PhoneNum", user.PhoneNum);
                cmd.SetParameterValue("@FaxNum", user.FaxNum);
                cmd.SetParameterValue("@Mobile", user.Mobile);
                cmd.SetParameterValue("@UpdateDate", user.UpdateDate);
                cmd.SetParameterValue("@CompanyName", company.CompanyName);
                cmd.SetParameterValue("@CnCompanyName", company.CnCompanyName);
                cmd.SetParameterValue("@CompanyEmail", company.CompanyEmail);
                cmd.SetParameterValue("@CountryID", company.CountryID);
                cmd.SetParameterValue("@IsExport", company.IsExport);
                cmd.SetParameterValue("@IsOE", company.IsOE);

                cmd.SetParameterValue("@MemberType", company.MemberType);
                cmd.SetParameterValue("@CreateIp", company.CreateIp);
                cmd.SetParameterValue("@UpdateIp", company.UpdateIp);
                cmd.SetParameterValue("@AuditStatus", company.AuditStatus);
                cmd.ExecuteNonQuery();

                user.UserID = (int)cmd.GetParameterValue("@UserID");
                user.CompanyID = (int)cmd.GetParameterValue("@CompanyID");
                company.CompanyID = user.CompanyID;
                return true;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
                return false;
            }
        }
    }
}
